package cn.iocoder.yudao.module.reading.dal.mysql.community;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.reading.dal.dataobject.community.CommunityPostLikeDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 社区帖子点赞 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface CommunityPostLikeMapper extends BaseMapperX<CommunityPostLikeDO> {

    /**
     * 查询用户对指定帖子的点赞记录（只查询未删除的）
     */
    default CommunityPostLikeDO selectByPostIdAndUserId(Long postId, Long userId) {
        return selectOne(CommunityPostLikeDO::getPostId, postId,
                        CommunityPostLikeDO::getUserId, userId);
    }

    /**
     * 物理删除用户对指定帖子的点赞记录
     */
    @Delete("DELETE FROM community_post_likes WHERE post_id = #{postId} AND user_id = #{userId}")
    void deleteByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId);

    /**
     * 批量查询用户对帖子列表的点赞状态
     */
    default List<CommunityPostLikeDO> selectByPostIdsAndUserId(List<Long> postIds, Long userId) {
        return selectList(new LambdaQueryWrapperX<CommunityPostLikeDO>()
                .in(CommunityPostLikeDO::getPostId, postIds)
                .eq(CommunityPostLikeDO::getUserId, userId));
    }

}
